package com.stylefeng.guns.core.util;

import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.exception.GunsExceptionEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import javax.servlet.http.HttpServletResponse;

public class FileUtil {

    private static Logger log = LoggerFactory.getLogger(FileUtil.class);

    /**
     * NIO way
     */
    public static byte[] toByteArray(String filename) {

        File f = new File(filename);
        if (!f.exists()) {
            log.error("文件未找到！" + filename);
            throw new GunsException(GunsExceptionEnum.FILE_NOT_FOUND);
        }
        FileChannel channel = null;
        FileInputStream fs = null;
        try {
            fs = new FileInputStream(f);
            channel = fs.getChannel();
            ByteBuffer byteBuffer = ByteBuffer.allocate((int) channel.size());
            while ((channel.read(byteBuffer)) > 0) {
                // do nothing
                // System.out.println("reading");
            }
            return byteBuffer.array();
        } catch (IOException e) {
            throw new GunsException(GunsExceptionEnum.FILE_READING_ERROR);
        } finally {
            try {
                channel.close();
            } catch (IOException e) {
                throw new GunsException(GunsExceptionEnum.FILE_READING_ERROR);
            }
            try {
                fs.close();
            } catch (IOException e) {
                throw new GunsException(GunsExceptionEnum.FILE_READING_ERROR);
            }
        }
    }

    /**
     * 删除目录
     *
     * @author fengshuonan
     * @Date 2017/10/30 下午4:15
     */
    public static boolean deleteDir(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            for (int i = 0; i < children.length; i++) {
                boolean success = deleteDir(new File(dir, children[i]));
                if (!success) {
                    return false;
                }
            }
        }
        return dir.delete();
    }
    
    //yyw 调用前先判断目 : 文件下载,只能下载指定目录下的文件
    public static void downloadFile(String filePath, HttpServletResponse response) {
    	
    	File file = new File(filePath);
    	if (file.isDirectory()) {
    		log.warn(file.getPath() + "是目录，不是文件，下载失败！");
    		return;
    	}
    	response.reset();
    	response.setCharacterEncoding("utf-8");
    	response.setContentType("octet-stream");
    	response.setHeader("Content_Length", file.length() + "");
    	InputStream is = null;
    	OutputStream os = null;
    	try {
    		response.setHeader("Content-Disposition",
    				"attachment;fileName=" + java.net.URLEncoder.encode(file.getName(), "UTF-8"));
    		is = new FileInputStream(file);
    		os = response.getOutputStream();
    		byte[] buff = new byte[1024];
    		int size;
    		while ((size = is.read(buff)) > 0) {
    			os.write(buff, 0, size);
    		}
    		os.flush();
    	} catch (Exception e) {
    		log.warn("", e);
    	} finally {
    		try {
    			if (os != null) {
    				os.close();
    			}
    			if (is != null) {
    				is.close();
    			}
    		} catch (IOException ioe) {
    			log.warn("Exception", ioe);
    		}
    	}
    	
    }
    
    //==
}